<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
                    xmlns:controlzEx="clr-namespace:ControlzEx;assembly=MaterialDesignThemes.Wpf">
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

  <ControlTemplate x:Key="MaterialDesignValidationErrorTemplate">
    <ControlTemplate.Resources>
      <DataTemplate DataType="{x:Type ValidationError}">
        <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                   FontSize="10"
                   MaxWidth="{Binding ElementName=Placeholder, Path=ActualWidth}"
                   HorizontalAlignment="Left"
                   Margin="2"
                   TextWrapping="Wrap"
                   Text="{Binding ErrorContent}"
                   UseLayoutRounding="false" />
      </DataTemplate>
    </ControlTemplate.Resources>
    <StackPanel>
      <DockPanel >
        <AdornedElementPlaceholder Name="Placeholder"/>
        <TextBlock Name="Asterisk"
                   Margin="4"
                   Text="*"
                   Foreground="{DynamicResource ValidationErrorBrush}"
                   ToolTip="{Binding ErrorContent}"
                   VerticalAlignment="Center"
                   Visibility="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress), Converter={StaticResource BooleanToVisibilityConverter}}"/>
      </DockPanel>
      <Border x:Name="DefaultErrorViewer"
              Visibility="Collapsed"
              Background="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}">
        <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                   FontSize="10"
                   MaxWidth="{Binding ElementName=Placeholder, Path=ActualWidth}"
                   HorizontalAlignment="Left"
                   Margin="0 2"
                   TextWrapping="Wrap"
                   Text="{Binding CurrentItem.ErrorContent}"
                   UseLayoutRounding="false" />
      </Border>
      <controlzEx:PopupEx x:Name="ValidationPopup"
                          IsOpen="False"
                          Placement="Bottom"
                          PlacementTarget="{Binding ElementName=Placeholder, Mode=OneWay}"
                          AllowsTransparency="True">
        <Border x:Name="PopupBorder" Background="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}">
          <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                     FontSize="10"
                     HorizontalAlignment="Left"
                     Margin="0 2"
                     TextWrapping="Wrap"
                     Text="{Binding CurrentItem.ErrorContent}"
                     UseLayoutRounding="false" />
        </Border>
      </controlzEx:PopupEx>
    </StackPanel>
    <ControlTemplate.Triggers>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="ValidationPopup" Property="IsOpen" Value="True"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="DefaultErrorViewer" Property="Visibility" Value="Visible"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="ValidationPopup" Property="IsOpen"
                            Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="DefaultErrorViewer" Property="Visibility"
                  Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <DataTrigger Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}" Value="{x:Null}">
        <Setter TargetName="PopupBorder" Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
        <Setter TargetName="DefaultErrorViewer" Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
      </DataTrigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <ControlTemplate x:Key="MaterialDesignValidationErrorNoAsteriskTemplate">
    <ControlTemplate.Resources>
      <DataTemplate DataType="{x:Type ValidationError}">
        <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                   FontSize="10"
                   MaxWidth="{Binding ElementName=Placeholder, Path=ActualWidth}"
                   HorizontalAlignment="Left"
                   Margin="2"
                   TextWrapping="Wrap"
                   Text="{Binding ErrorContent}"
                   UseLayoutRounding="false" />
      </DataTemplate>
    </ControlTemplate.Resources>
    <StackPanel>
      <AdornedElementPlaceholder Name="Placeholder"/>
      <Border x:Name="DefaultErrorViewer"
              Visibility="Collapsed"
              Background="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}">
        <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                   FontSize="10"
                   MaxWidth="{Binding ElementName=Placeholder, Path=ActualWidth}"
                   HorizontalAlignment="Left"
                   Margin="0 2"
                   TextWrapping="Wrap"
                   Text="{Binding CurrentItem.ErrorContent}"
                   UseLayoutRounding="false" />
      </Border>
      <controlzEx:PopupEx x:Name="ValidationPopup"
                          IsOpen="False"
                          Placement="Bottom"
                          PlacementTarget="{Binding ElementName=Placeholder, Mode=OneWay}"
                          AllowsTransparency="True">
        <Border x:Name="PopupBorder" Background="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}">
          <TextBlock Foreground="{DynamicResource ValidationErrorBrush}"
                     FontSize="10"
                     HorizontalAlignment="Left"
                     Margin="0 2"
                     TextWrapping="Wrap"
                     Text="{Binding CurrentItem.ErrorContent}"
                     UseLayoutRounding="false" />
        </Border>
      </controlzEx:PopupEx>
    </StackPanel>
    <ControlTemplate.Triggers>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="ValidationPopup" Property="IsOpen" Value="True"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="DefaultErrorViewer" Property="Visibility" Value="Visible"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="ValidationPopup" Property="IsOpen"
                            Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True"/>
          <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter TargetName="DefaultErrorViewer" Property="Visibility"
                  Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>

      <DataTrigger Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Background)}" Value="{x:Null}">
        <Setter TargetName="PopupBorder" Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
        <Setter TargetName="DefaultErrorViewer" Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
      </DataTrigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</ResourceDictionary>
